package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.Score;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.PeUtil;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.SizeUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 我的积分
public class MyScoreActivity extends Base2Activity {
    private RecyclerView rcv_list;// 积分列表

    private MyScoreListAdapter adapter;
    private List<Score> scoreList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_score;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_list = findViewById(R.id.rcv_list);

        setHeadTitle("我的积分");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new MyScoreListAdapter(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        rxList.add(reqDataList());
        showProgress();
    }

    private Disposable reqDataList() {
        return APIRetrofit.getLs().reqUserScore(Global.HEADER)
                .compose(RxUtil.<BaseResult2<List<Score>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Score>>>() {
                    @Override
                    public void accept(BaseResult2<List<Score>> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {

                        } else {

                        }
                        scoreList = result2.getData();
                        adapter.setDataList(scoreList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class MyScoreListAdapter extends ListBaseAdapter<Score> {

        public MyScoreListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_my_score_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RelativeLayout rl_item = holder.getView(R.id.rl_item);
            TextView tv_title = holder.getView(R.id.tv_title);
            TextView tv_num = holder.getView(R.id.tv_num);
            TextView tv_time = holder.getView(R.id.tv_time);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) rl_item.getLayoutParams();
            if (position == 0) {
                params.topMargin = SizeUtils.dp2px(32);
                params.bottomMargin = SizeUtils.dp2px(16);
            } else if (position == mDataList.size() - 1) {
                params.topMargin = SizeUtils.dp2px(16);
                params.bottomMargin = SizeUtils.dp2px(32);
            } else {
                params.topMargin = SizeUtils.dp2px(16);
                params.bottomMargin = SizeUtils.dp2px(16);
            }

            Score score = mDataList.get(position);
            String title = score.getOrder_num() + PeUtil.getScoreStatus(score.getType());
            tv_title.setText(title);// // 积分来源 0 订单付款 1 退款 2 商城下单 3 商城取消支付
            String num = (TextUtils.equals("0", score.getStatus()) ? "＋" : "－") + score.getScore();
            tv_num.setText(num);
        }
    }
}